/***************************************************************************************************/
/* @date on 2020/08/28                                                                             */
/* @author Xie Jian                                                                                */
/* @email 190954950@qq.com                                                                         */
/* @describe 文件处理功能                                                                           */
/***************************************************************************************************/
use std::fs::File;
use std::io::ErrorKind;

/// 文件是否存在
///
/// # Examples
///
/// ```
/// let b = is_exist("文件路径"); //b 返回是否存在文件
/// ```
pub fn is_exist(path: &str) -> bool {
    let f = File::open(path); //.unwrap();
    match f {
        Ok(_file) => {
            //println!("File opened successfully.");
            return true;
        }
        Err(error) => match error.kind() {
            ErrorKind::NotFound => false,
            _other_error => false,
        },
    }
}

/// 创建文件
///
/// # Examples
///
/// ```
/// let b = create_file("文件路径"); //b 返回是否创建成功
/// ```
pub fn create_file(path: &str) -> bool {
    let b = is_exist(path);
    if b {
        return false;
    } else {
        let r = File::create(path);
        match r {
            Ok(_data) => {
                return true;
            }
            Err(_error) => {
                return false;
            }
        }
    }
}